package cn.com.api.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiU_match_prize;
import cn.com.api.entity.ApiU_match_vote;
import cn.com.api.mapper.ApiU_match_prizeMapper;
import cn.com.api.mapper.ApiU_match_voteMapper;
import cn.com.api.mapper.ApiU_match_worksMapper;
import cn.com.api.service.ApiU_match_voteService;
import cn.com.api.service.impl.base.U_match_voteServiceImplBase;

/**
 * 
 **/
@Service
public class ApiU_match_voteServiceImpl extends U_match_voteServiceImplBase implements ApiU_match_voteService{
	@Autowired
	private ApiU_match_voteMapper apiU_match_voteMapper;
	
	@Autowired
	private ApiU_match_worksMapper apiU_match_worksMapper;
	
	@Autowired
	private ApiU_match_prizeMapper apiU_match_prizeMapper ;
    
	
     	//新增对象后返回记录ID
    public int U_match_voteInsertion(ApiU_match_vote u_match_vote){
        return this.apiU_match_voteMapper.U_match_voteInsertion(u_match_vote);
    }


	@Override
	public List<ApiU_match_vote> findU_match_voteByWorkId(Integer match_works_id) {
		
		return apiU_match_voteMapper.findU_match_voteByWorkId(match_works_id);
	}


	@Override
	public int findU_vote_numByMatchId(Integer match_id) throws Exception {
		
		return apiU_match_voteMapper.findU_vote_numByMatchId(match_id);
	}


	@Override
	public List<ApiU_match_vote> findU_match_votesByWorksId(String match_works_id)
			throws Exception {
		
		return apiU_match_voteMapper.findU_match_votesByWorksId(match_works_id);
	}


	@Override
	public List<ApiU_match_vote> findU_match_votesByMatchId(String match_id)
			throws Exception {
		List<ApiU_match_prize> list_prize = apiU_match_prizeMapper.findU_match_prizesByMatchId(match_id);
		List<ApiU_match_vote> list = apiU_match_voteMapper.findU_match_votesByMatchId(match_id);
		for (ApiU_match_vote apiU_match_vote : list) {
			int ranking = apiU_match_vote.getRanking();
			//奖金金额暂定处理,后面需要到存储过程中最好
			apiU_match_vote.setExt3("" + getU_wokrs_bonus(ranking, match_id, list_prize));
		}
		
		return list;
	}
 
	//获取奖金金额
	private Long getU_wokrs_bonus(int ranking, String match_id, List<ApiU_match_prize>  list) {
		
		long prize_money_result = 0;
		int prize_num_sum = 0;
		for (ApiU_match_prize apiU_match_prize : list) {
			prize_num_sum += apiU_match_prize.getWinner_count();
			if(ranking <= prize_num_sum){
				prize_money_result = apiU_match_prize.getPrize_money()/apiU_match_prize.getWinner_count();
				break;
			}
		}
		return prize_money_result;
	}
}


